package org.jeecg.modules.yw.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.yw.entity.EnterPrise;
import org.jeecg.modules.yw.entity.ProjectWindows;
import org.jeecg.modules.yw.service.EnterPriseService;
import org.jeecg.modules.yw.service.ProjectWindowsService;
import org.jeecg.modules.yw.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: project_windows
 * @Author: jeecg-boot
 * @Date: 2025-05-10
 * @Version: V1.0
 */
@Api(tags = "project_windows")
@RestController
@RequestMapping("/yw/projectWindows")
@Slf4j
public class ProjectWindowsController extends JeecgController<ProjectWindows, ProjectWindowsService> {
    @Autowired
    private ProjectWindowsService projectWindowsService;

    @Autowired
    private EnterPriseService enterpriseService;
    @Resource
    private ISysBaseAPI sysBaseAPI;
    /**
     * 分页列表查询
     *
     * @param
     * @param pageNo
     * @param pageSize
     * @param
     * @return
     */
    //@AutoLog(value = "project_windows-分页列表查询")
    @ApiOperation(value = "project_windows-分页列表查询", notes = "project_windows-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<ProjectWindows>> queryPageList(@RequestParam(name = "id", required = false)String id, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        QueryWrapper<ProjectWindows> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(id)){
            queryWrapper.eq("id",id);
        }
        Page<ProjectWindows> page = new Page<>(pageNo, pageSize);
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<String> roleIds = sysBaseAPI.getRoleIdsByUsername(sysUser.getUsername());
        if (!roleIds.contains(CommonUtils.ADMIN)) {
            String area=sysUser.getArea();
            if(StringUtils.isNotBlank(area)){
                String[] areas=area.split(",");
                StringBuffer st=new StringBuffer();
                for (int i = 0; i < areas.length; i++) {
                    st.append("\""+areas[i]+"\",");
                }
                st=st.deleteCharAt(st.length()-1);
                String sql="(select id from enterprise where city_code in ("+st.toString()+") " +
                        "or (province_code in  ("+st.toString()+") and city_code is null) )";
                queryWrapper.apply("enterpriseId in"+sql);
            }else{
                IPage<ProjectWindows> result =new Page<>();
                return Result.ok(result);
            }
        }
        queryWrapper.orderByDesc("talkTime");
        IPage<ProjectWindows> pageList = projectWindowsService.page(page, queryWrapper);
        if(pageList.getRecords()!=null  && pageList.getRecords().size()>0){
            int  size = pageList.getRecords().size();
            for (int i = 0; i < size; i++) {
                ProjectWindows pw= pageList.getRecords().get(i);
                if(pw.getEnterpriseId()!=null){
                    EnterPrise enterprise =enterpriseService.getById(pw.getEnterpriseId());
                    pw.setEname(enterprise==null?"" :enterprise.getName());
                }
            }
        }
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param projectWindows
     * @return
     */
    @AutoLog(value = "project_windows-添加")
    @ApiOperation(value = "project_windows-添加", notes = "project_windows-添加")
    //@RequiresPermissions("org.jeecg.modules:project_windows:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody ProjectWindows projectWindows) {
        projectWindowsService.save(projectWindows);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param projectWindows
     * @return
     */
    @AutoLog(value = "project_windows-编辑")
    @ApiOperation(value = "project_windows-编辑", notes = "project_windows-编辑")
    //@RequiresPermissions("org.jeecg.modules:project_windows:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody ProjectWindows projectWindows) {
        projectWindowsService.updateById(projectWindows);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "project_windows-通过id删除")
    @ApiOperation(value = "project_windows-通过id删除", notes = "project_windows-通过id删除")
    //@RequiresPermissions("org.jeecg.modules:project_windows:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        projectWindowsService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "project_windows-批量删除")
    @ApiOperation(value = "project_windows-批量删除", notes = "project_windows-批量删除")
    //@RequiresPermissions("org.jeecg.modules:project_windows:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.projectWindowsService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "project_windows-通过id查询")
    @ApiOperation(value = "project_windows-通过id查询", notes = "project_windows-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<ProjectWindows> queryById(@RequestParam(name = "id", required = true) String id) {
        ProjectWindows projectWindows = projectWindowsService.getById(id);
        if (projectWindows == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(projectWindows);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param projectWindows
     */
    //@RequiresPermissions("org.jeecg.modules:project_windows:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ProjectWindows projectWindows) {
        return super.exportXls(request, projectWindows, ProjectWindows.class, "project_windows");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("project_windows:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ProjectWindows.class);
    }

}
